package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    AddressBookService addressBookService;

    //用于新增地址
    @PostMapping
    public R save(@RequestBody AddressBook addressBook, HttpSession hs) {
        log.info("操作用户id为{},添加地址信息为{}", hs.getAttribute("user"), addressBook.toString());
        addressBook.setUserId((Long) hs.getAttribute("user"));
        addressBookService.save(addressBook);
        return R.success("保存地址成功!");
    }

    //用于设置默认地址
    @PutMapping("/default")
    public R setDefault(@RequestBody AddressBook addressBook, HttpSession hs) {
        log.info("要设置的默认地址id为{}", addressBook.getId());


        //全部设置为0
        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.set(AddressBook::getIsDefault, 0)
                .eq(AddressBook::getUserId, hs.getAttribute("user"));
        addressBookService.update(qw);

        //单独设置为1
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置成功!");
    }

    //用于根据id查询地址
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        log.info("要查询的地址id为{}", id);
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return R.success(addressBook);
        }
        return R.error("没有查询到对应地址信息");
    }

    //用于查询默认地址
    @GetMapping("/default")
    public R getByDefault() {
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getIsDefault, 1);
        AddressBook one = addressBookService.getOne(qw);
        log.info("默认地址信息为{}", one.toString());
        return R.success(one);
    }

    //用于查询指定用户地址
    @GetMapping("/list")
    public R getList(HttpSession hs) {
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId, hs.getAttribute("user"));
        List<AddressBook> list = addressBookService.list(qw);
        log.info("默认地址信息为{}", list.toString());
        return R.success(list);
    }

    //用于删除地址
    @DeleteMapping
    public R deleteByIds(@RequestParam List<Long> ids) {
        log.info("要删除的id为{}",ids);
        addressBookService.removeByIds(ids);
        return R.success("删除成功!");
    }

    //用于修改地址
    @PutMapping
    public R update(@RequestBody AddressBook addressBook){
        log.info("要修改的用户为{}",addressBook.toString());
        addressBookService.updateById(addressBook);
        return R.success("修改成功!");
    }
}
